package cn.bs.busuo.account.service.impl;


import cn.bs.busuo.account.dao.persist.mapper.CityMapper;
import cn.bs.busuo.account.dao.persist.repository.ICityRepository;
import cn.bs.busuo.account.dao.persist.repository.ISightRepository;
import cn.bs.busuo.account.dao.persist.repository.ISightsSeasonsRepository;
import cn.bs.busuo.account.pojo.entity.CitySights;
import cn.bs.busuo.account.pojo.entity.Sight;
import cn.bs.busuo.account.pojo.entity.SightsSeasons;
import cn.bs.busuo.account.pojo.param.City;
import cn.bs.busuo.account.pojo.param.Seasons;
import cn.bs.busuo.account.pojo.param.SightAdd;
import cn.bs.busuo.account.pojo.param.SightUpdate;
import cn.bs.busuo.account.pojo.vo.CitiesVO;
import cn.bs.busuo.account.pojo.vo.SeasonVO;
import cn.bs.busuo.account.pojo.vo.SightListVO;
import cn.bs.busuo.account.pojo.vo.SightVO;
import cn.bs.busuo.account.service.ISightService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SightServiceImpl implements ISightService {

    @Autowired
    ISightRepository sightRepository;
    @Autowired
    private ICityRepository cityRepository;
    @Autowired
    ISightsSeasonsRepository sightsSeasonsRepository;

    //添加景点
    @Override
    public void addSight(SightAdd sightAdd, Seasons seasons, City city) {
        //处理景点表
        Sight sight = new Sight();
        BeanUtils.copyProperties(sightAdd, sight);
        String sightName = sight.getSightName();
        SightVO sightById = sightRepository.getSightByName(sightName);
//        如果根据景点名字查询出数据不为空,并且显示
        if (sightById != null && sightById.getEnable() == 1) {
            String message = "新增景点失败，该景点已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
//        如果查询出数据不为空,但是不显示,则把Enable改为1
        else if (sightById != null && sightById.getEnable() == 0) {
            SightUpdate sightUpdate = new SightUpdate();
            sightUpdate.setId(sightById.getId());
            sightUpdate.setEnable(1L);
            sightRepository.delectSight(sightUpdate);
        }
//        查询出为null,则重新添加
        else {
            sight.setEnable(1L);
            sightRepository.addSight(sight);
        }

//        处理季节推荐表
        //        根据传入的用户名查询景点的ID,然后传入景点ID,季节ID
        SeasonVO seasonVO = sightsSeasonsRepository.selectById(seasons.getBestSeasonId());
        if (seasonVO == null) {
            String message = "你输入的季节id不存在,请重新输入！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        String sightName1 = sightAdd.getSightName();
        SightVO sightById1 = sightRepository.getSightByName(sightName1);
        Long id = sightById1.getId();
        SightsSeasons sightsSeasons = new SightsSeasons();
        BeanUtils.copyProperties(seasons, sightsSeasons);
        sightsSeasons.setSightsId(id);
//        向季节推荐表中插入数据
        sightsSeasonsRepository.addSeason(sightsSeasons);

//        向城市景点表插入数据
        CitySights citySights = new CitySights();
//        拿到城市ID
        BeanUtils.copyProperties(city, citySights);
//        根据景点名字查出景点数据
        SightVO sightByName = sightRepository.getSightByName(sightAdd.getSightName());
//        将景点id给到citySights
        citySights.setSightId(sightByName.getId());
        //         判断传入的城市id是否存在,
        CitiesVO citiesVO = cityRepository.selectById(citySights.getCityId());
        if (citiesVO == null) {
            String message = "新增景点失败，该景点所属的城市不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        cityRepository.insert(citySights);


    }

    //删除景点
    @Override
    public void delectSight(Long id) {
        SightUpdate sightUpdate = new SightUpdate();
        sightUpdate.setEnable(0L);
        sightUpdate.setId(id);
        sightRepository.delectSight(sightUpdate);
    }

    //根据名字查找景点
    @Override
    public SightVO getSightByName(String name) {
        return sightRepository.getSightByName(name);
    }

    //查询景点列表
    @Override
    public PageData<SightListVO> selectList(Integer pageNum, Integer pageSize) {
        return sightRepository.selectList(pageNum, pageSize);
    }

    // 根据季节查询景点
    @Override
    public List<SightListVO> selcteSeason(Long seasonId) {
        List<SightListVO> sightListVOS = sightRepository.selcteSeason(seasonId);
        if (sightListVOS.size() == 0) {
            String message = "该季节暂时没有推荐的景点！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return sightListVOS;
    }

    //根据城市id查询景点
    @Override
    public List<SightListVO> selectList(Long id) {
        List<SightListVO> sightListVOS = cityRepository.selectList(id);
        if (sightListVOS.size() == 0) {
            String message = "该城市暂时还未添加景点！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        return sightListVOS;
    }
}